package sdk

import (
	httpUtils "demo/kongganghelper/http_utils"
	"demo/kongganghelper/sdk/model"
	"demo/kongganghelper/utils"
	"encoding/json"
	log "github.com/sirupsen/logrus"
	"strconv"
)

// GetHomeRecommend 获取首页推广
func GetHomeRecommend(user *model.User) []model.Good {
	log.Info("请求拉取首页推广")
	url := "/api/product/index/recommend"
	params := make(map[string]string)
	params["frameType"] = "0"

	params["branchNo"] = user.BranchNo
	params["fundAccount"] = user.FundAccount
	params["token"] = user.Token
	params["uid"] = strconv.Itoa(user.UserId)

	parametersProcessing(params)
	res, err := httpUtils.Get(urlProcessing(url), params)
	if err != nil {
		panic(err)
		return nil
	}
	log.Info(res)
	r := &model.Response{}
	err = json.Unmarshal([]byte(res), r)
	if err != nil {
		panic(err)
		return nil
	}

	data := r.Data.([]interface{})
	goods := make([]model.Good, len(data))
	err = utils.CovertJson(&data, &goods)
	if err != nil {
		panic(err)
		return nil
	}
	log.Info(goods)
	return goods
}

// Login 登录
func Login(uInfo string, password string) *model.User {
	log.Infof("[%s]登录空港服务器...", uInfo)
	url := "/login/V2.decryption"

	params := make(map[string]string)
	params["uinfo"] = uInfo
	params["password"] = password

	parametersProcessing(params)

	res, err := httpUtils.Post(urlProcessing(url), params)
	if err != nil {
		log.Error(err)
		return nil
	}
	log.Info(res)
	r := &model.Response{}
	err = json.Unmarshal([]byte(res), r)
	if err != nil {
		log.Error(err)
		return nil
	}
	log.Info(res)
	if r.Message.Code != 0 {
		log.Errorf("用户登录失败，响应：[%s]", r.Message.Message)
		return nil
	}
	m := r.Data.(map[string]interface{})
	value, ok := m["user"]
	if !ok {
		return nil
	}
	u := &model.User{}
	err = utils.CovertJson(value, u)
	if err != nil {
		log.Error(err)
		return nil
	}
	log.Info(u)
	return u
}

func GetProductTypeList() {

	log.Info("请求拉取产品分类列表")
	url := "/api/product/typeList"
	params := make(map[string]string)
	params["_s_q__"] = "aaa"

	parametersProcessing(params)
	res, err := httpUtils.Get(urlProcessing(url), params)
	if err != nil {
		log.Error(err)
		return
	}
	log.Info(res)
	r := &model.Response{}
	err = json.Unmarshal([]byte(res), r)
	if err != nil {
		log.Error(err)
		return
	}
	log.Info(r)
}

func GetShopCard(user *model.User) {
	url := "/api/maojiuhui/shoppingCart/summary"
	params := make(map[string]string)
	params["_s_q__"] = "aaa"
	params["branchNo"] = user.BranchNo
	params["fundAccount"] = user.FundAccount
	params["token"] = user.Token
	params["uid"] = strconv.Itoa(user.UserId)

	parametersProcessing(params)
	res, err := httpUtils.Get(urlProcessing(url), params)
	if err != nil {
		log.Error(err)
		return
	}
	log.Info(res)
	r := &model.Response{}
	err = json.Unmarshal([]byte(res), r)
	if err != nil {
		log.Error(err)
		return
	}
	log.Info(r)
}

func CostCalculation(user *model.User, good model.Good, shares int) {
	url := "/api/transaction/costCalculation"

	params := make(map[string]string)
	params["_s_q__"] = "aaa"
	params["branchNo"] = user.BranchNo
	params["fundAccount"] = user.FundAccount
	params["token"] = user.Token
	params["uid"] = strconv.Itoa(user.UserId)

	params["erpCode"] = good.ErpCode
	params["price"] = strconv.Itoa(good.Price)
	params["shares"] = strconv.Itoa(shares)

	parametersProcessing(params)

	res, err := httpUtils.Post(urlProcessing(url), params)
	if err != nil {
		log.Error(err)
		return
	}
	log.Info(res)
	r := &model.Response{}
	err = json.Unmarshal([]byte(res), r)
	if err != nil {
		log.Error(err)
		return
	}
}

func GetAddress(user *model.User) []model.Address {
	url := "/api/maojiuhui/address/query"
	params := make(map[string]string)
	params["_s_q__"] = "aaa"
	params["branchNo"] = user.BranchNo
	params["fundAccount"] = user.FundAccount
	params["token"] = user.Token
	params["uid"] = strconv.Itoa(user.UserId)

	parametersProcessing(params)
	res, err := httpUtils.Get(urlProcessing(url), params)
	if err != nil {
		log.Error(err)
		return nil
	}
	log.Info(res)
	r := &model.Response{}
	err = json.Unmarshal([]byte(res), r)
	if err != nil {
		log.Error(err)
		return nil
	}

	i := r.Data.([]interface{})
	addresses := make([]model.Address, len(i))
	err = utils.CovertJson(&i, &addresses)
	if err != nil {
		log.Error(err)
		return nil
	}
	log.Info(addresses)
	return addresses
}

func PaymentAdditional(user *model.User) {
	url := "/api/product/payment/additional"
	params := make(map[string]string)
	params["_s_q__"] = "bbb"
	params["branchNo"] = user.BranchNo
	params["fundAccount"] = user.FundAccount
	params["token"] = user.Token
	params["uid"] = strconv.Itoa(user.UserId)

	parametersProcessing(params)

	res, err := httpUtils.Get(urlProcessing(url), params)
	if err != nil {
		log.Error(err)
		return
	}
	r := &model.Response{}
	err = json.Unmarshal([]byte(res), r)
	if err != nil {
		log.Error(err)
		return
	}
	log.Info(res)
	//i := r.Data.([]interface{})
	//addresses := make([]model.Address, len(i))
	//err = utils.CovertJson(&i, &addresses)
	//if err != nil {
	//	log.Error(err)
	//	return
	//}
	//log.Info(addresses)
}

func ProductBuyLimit(user *model.User) {
	url := "/api/user/productBuyLimit"
	params := make(map[string]string)
	params["_s_q__"] = "bbb"
	params["branchNo"] = user.BranchNo
	params["fundAccount"] = user.FundAccount
	params["token"] = user.Token
	params["uid"] = strconv.Itoa(user.UserId)

	parametersProcessing(params)

	res, err := httpUtils.Get(urlProcessing(url), params)
	if err != nil {
		log.Error(err)
		return
	}
	log.Info(res)
	r := &model.Response{}
	err = json.Unmarshal([]byte(res), r)
	if err != nil {
		log.Error(err)
		return
	}
}

func PurchaseExchange(user *model.User) {
	url := "/api/shoppingScore/purchaseExchange"
	params := make(map[string]string)
	params["_s_q__"] = "bbb"
	params["branchNo"] = user.BranchNo
	params["fundAccount"] = user.FundAccount
	params["token"] = user.Token
	params["uid"] = strconv.Itoa(user.UserId)

	parametersProcessing(params)

	res, err := httpUtils.Get(urlProcessing(url), params)
	if err != nil {
		log.Error(err)
		return
	}
	log.Info(res)
	r := &model.Response{}
	err = json.Unmarshal([]byte(res), r)
	if err != nil {
		log.Error(err)
		return
	}
}

func GetProductDetail(user *model.User, good model.Good) {
	url := "/api/product/detail/V1"
	params := make(map[string]string)
	params["_s_q__"] = "bbb"
	params["branchNo"] = user.BranchNo
	params["fundAccount"] = user.FundAccount
	params["token"] = user.Token
	params["uid"] = strconv.Itoa(user.UserId)
	params["erpCode"] = good.ErpCode
	params["price"] = strconv.Itoa(good.Price)
	params["shares"] = "1"

	parametersProcessing(params)

	res, err := httpUtils.Get(urlProcessing(url), params)
	if err != nil {
		log.Error(err)
		return
	}
	log.Info(res)
	r := &model.Response{}
	err = json.Unmarshal([]byte(res), r)
	if err != nil {
		log.Error(err)
		return
	}
}

func GetProductDetail0(user *model.User) {
	url := "/api/product/list"
	params := make(map[string]string)
	params["_s_q__"] = "bbb"
	params["branchNo"] = user.BranchNo
	params["fundAccount"] = user.FundAccount
	params["token"] = user.Token
	params["uid"] = strconv.Itoa(user.UserId)

	params["pageSize"] = "50"
	params["currentPage"] = "2"
	params["brand"] = "251"

	parametersProcessing(params)

	res, err := httpUtils.Get(urlProcessing(url), params)
	if err != nil {
		log.Error(err)
		return
	}
	log.Info(res)
	r := &model.Response{}
	err = json.Unmarshal([]byte(res), r)
	if err != nil {
		log.Error(err)
		return
	}
}

func CheckRestrictedArea(user *model.User, good model.Good, address model.Address) {
	url := "/api/transaction/purchase/checkRestrictedArea"

	params := make(map[string]string)
	params["_s_q__"] = "bbb"
	params["branchNo"] = user.BranchNo
	params["fundAccount"] = user.FundAccount
	params["token"] = user.Token
	params["uid"] = strconv.Itoa(user.UserId)

	params["erpCode"] = good.ErpCode
	params["orderType"] = "11" //???
	params["addressId"] = strconv.Itoa(address.Id)
	params["wineType"] = "0"

	parametersProcessing(params)

	res, err := httpUtils.Post(urlProcessing(url), params)
	if err != nil {
		log.Error(err)
		return
	}
	log.Info(res)
	r := &model.Response{}
	err = json.Unmarshal([]byte(res), r)
	if err != nil {
		log.Error(err)
		return
	}
}

func PurchasePlaceOrder(user *model.User, good model.Good, address model.Address, amount int) {
	url := "/api/transaction/purchasePlaceOrder.decryption"

	params := make(map[string]string)
	params["_s_q__"] = "bbb"
	params["branchNo"] = user.BranchNo
	params["fundAccount"] = user.FundAccount
	params["token"] = user.Token
	params["uid"] = strconv.Itoa(user.UserId)

	params["erpCode"] = good.ErpCode
	params["orderType"] = "11" //???
	params["addressId"] = strconv.Itoa(address.Id)
	params["amount"] = strconv.Itoa(amount)
	params["captchaId"] = "588b4c756d4443f4b9d156edf7330b0d" //验证码的md5
	params["expressCost"] = "0"
	params["insuranceCost"] = "0"
	params["packCost"] = "0"
	params["payType"] = "-128" //??
	params["paymentAmount"] = strconv.Itoa(good.Price)
	params["productAmount"] = strconv.Itoa(good.Price)
	params["score"] = "0"
	params["shoppingScore"] = strconv.Itoa(good.Price)
	params["usePlusPrice"] = "0"
	params["validate"] = "CN31_tPWoHT0Rt2JduLISn5D-qqdGxSAXXlEmv1A2irQFVSZOBp8M6e6aMrybAm48svPxwHL-GTuaKhIwsqMxy2RN86CuMCsfi5hjGxivnXqxB.lT2UlCx9Mg0FdSuZCaO6zutuQis5wkukJex67u0qySe4sQcGhD-lqTNLwu_2YQpFSn8k7GD99RJxlbiVRw4yYKjjisEkIndkrC15yO_zLcfFQCPubvJGbzB6yrynq0wUmYRMBsbbgodqAIvZpLHeOVse-wkeGyxa6ljYnFuew.lZipJBDenEq4zZeWcFUeYb5lONVkmgnwB0uvLuHsvu4zmDEkybQ214pEzMte_kt5PmfPu4ZwA0XoKCw8WVbGRhuHuRUQzJoTcPYKgKQV-8sGuVfWjIKlRMUsV.8O8R_I.MErY6Ke10wNSoV1lsPKZdds-COYxCG6WAPEr0nTdSglrlLYHFV7e-J4MLfNMcO1fKsk4ieY5xgWqD1mHVoBEihF_MlaSqIZKABB75A3"
	params["wineType"] = "0"

	parametersProcessing(params)

	res, err := httpUtils.Post(urlProcessing(url), params)
	if err != nil {
		log.Error(err)
		return
	}
	log.Info(res)
	r := &model.Response{}
	err = json.Unmarshal([]byte(res), r)
	if err != nil {
		log.Error(err)
		return
	}
}
